#include "SdkNightMonitor.h"

#define DEF_TAG         "Night"

Com_u8 g_SdkNightMonitor_NeedSwitchMode = DEF_COM_TRUE;
EnSdkNightMode g_SdkNightMonitor_WorkMode = NIGHT_MODE_AUTO;

void Ext_SdkNightMonitor_SwitchMode(Com_u8 flag)
{
    g_SdkNightMonitor_NeedSwitchMode = flag;
}

void Ext_SdkNightMonitor_SetWorkMode(EnSdkNightMode flag)
{
    g_SdkNightMonitor_WorkMode = flag;
}

void SdkNightMonitor_SetIRLedAndIRCut(int mode)
{
    IMPVI_NUM vinum = IMPVI_MAIN;
    IMPISPRunningMode isp_mode;

    /* mode swtich */
    if (mode == NIGHT_MODE_ON)
    {
        isp_mode = IMPISP_RUNNING_MODE_NIGHT;
        IMP_ISP_Tuning_SetISPRunningMode(vinum, &isp_mode);
        Ext_SdkMediaCom_SetIRcutAndIRled(NIGHT_MODE_ON);
    }
    else if (mode == NIGHT_MODE_OFF)
    {
        Ext_SdkMediaCom_SetIRcutAndIRled(NIGHT_MODE_OFF);
        isp_mode = IMPISP_RUNNING_MODE_DAY;
        IMP_ISP_Tuning_SetISPRunningMode(vinum, &isp_mode);
    }
}

Com_void ChipSdk_set_night_param(pSChipSdkNightParam param)
{
    /*
     * night luma <
     * night luma_scence >
     * day luma >
     * day luma_scence <
     * day ExposureValue >
    */

#if 0
    param->night_luma = 33;
    param->night_luma_scence = 5526512;
    param->day_luma = 50;
    param->day_luma_scence = 10888;
    param->min_AeAGain = 1388;
    param->max_AeAGain = 2048;
    param->ExposureValue = 1388;
#endif

    param->night_luma = 42;
    param->night_luma_scence = 388888;
    param->day_luma = 46;
    param->day_luma_scence = 43888;
    param->min_AeAGain = 1024;
    param->max_AeAGain = 5120;
    param->ExposureValue = 1388;
}

void *SdkNightMonitor_Thread(void* arg)
{
    int ret = 0;
    int luma = 0, luma_scence = 0;
    int cnt = 0;
    int pre_status = DAY_MODE_SWITCH;
    IMPVI_NUM vinum = IMPVI_MAIN;
    IMPISPAEExprInfo ExpInfo;
    IMPISPAEScenceAttr AeSenAttr;
    SChipSdkNightParam param;

#if 0
    float gb_gain, gr_gain;
    IMPISPAWBGlobalStatisInfo wb;
#endif

    memset(&param,0,sizeof(param));

    ChipSdk_set_night_param(&param);
    SdkNightMonitor_SetIRLedAndIRCut(NIGHT_MODE_OFF);

    while (Ext_InComFile_Access(DEF_FLAG_SYNC_TIME_OK) == DEF_COM_FILE_NOT_EXIST)
    {
        usleep(100 * 1000);
    }

    DEF_COM_SET_THREAD_NAME("Night_Monitor",g_ComThread_Num);

    while(g_ComThread_Running)
    {
        if ((g_SdkNightMonitor_WorkMode == NIGHT_MODE_ON) && (g_SdkNightMonitor_NeedSwitchMode == DEF_COM_TRUE))
        {
            SdkNightMonitor_SetIRLedAndIRCut(NIGHT_MODE_ON);
            g_SdkNightMonitor_NeedSwitchMode = DEF_COM_FALSE;
        }
        else if ((g_SdkNightMonitor_WorkMode == NIGHT_MODE_OFF) && (g_SdkNightMonitor_NeedSwitchMode == DEF_COM_TRUE))
        {
            SdkNightMonitor_SetIRLedAndIRCut(NIGHT_MODE_OFF);
            g_SdkNightMonitor_NeedSwitchMode = DEF_COM_FALSE;
        }
        else if(g_SdkNightMonitor_WorkMode == NIGHT_MODE_AUTO)
        {
            /* Force switch back to day mode */
            if (g_SdkNightMonitor_NeedSwitchMode == DEF_COM_TRUE)
            {
                g_SdkNightMonitor_NeedSwitchMode = DEF_COM_FALSE;
                if(pre_status == NIGHT_MODE_SWITCH)
                {
                    PRT_NORMAL("reset night mode\n");
                    SdkNightMonitor_SetIRLedAndIRCut(NIGHT_MODE_ON);
                }
                else if(pre_status == DAY_MODE_SWITCH)
                {
                    PRT_NORMAL("reset day mode\n");
                    SdkNightMonitor_SetIRLedAndIRCut(NIGHT_MODE_OFF);
                }
            }

            ret = IMP_ISP_Tuning_GetAeExprInfo(vinum, &ExpInfo);
            if (ret == 0)
            {
                //PRT_NORMAL("ExposureValue: %lld\n", ExpInfo.ExposureValue);
                //PRT_NORMAL("AGain: %d,DGain: %d\n", ExpInfo.AeAGain,ExpInfo.AeDGain);
            }

            ret = IMP_ISP_Tuning_GetAeScenceAttr(vinum, &AeSenAttr);
            if(ret == 0)
            {
                luma = AeSenAttr.luma;
                luma_scence = AeSenAttr.luma_scence;
                //PRT_NORMAL("Luma: %d, Luma Scence: %d\n", AeSenAttr.luma, AeSenAttr.luma_scence);
            }

            if((pre_status == DAY_MODE_SWITCH) && (luma_scence >= param.night_luma_scence) && (luma <= param.night_luma))
            {
                if(cnt >= 10)
                {
                    PRT_INFO(DEF_TAG,"set night mode\n");
                    SdkNightMonitor_SetIRLedAndIRCut(NIGHT_MODE_ON);
                    pre_status = NIGHT_MODE_SWITCH;
                    cnt = 0;
                    usleep(3000 * 1000);
                    continue;
                }
                cnt++;
            }
            else if ((pre_status == NIGHT_MODE_SWITCH) && (luma_scence <= param.day_luma_scence) && (luma >= param.day_luma) && (ExpInfo.ExposureValue >= param.ExposureValue) && (ExpInfo.AeAGain >= param.min_AeAGain) && (ExpInfo.AeAGain <= param.max_AeAGain))
            {
                if(cnt >= 10)
                {
                    PRT_INFO(DEF_TAG,"set day mode\n");
                    SdkNightMonitor_SetIRLedAndIRCut(NIGHT_MODE_OFF);
                    pre_status = DAY_MODE_SWITCH;
                    cnt = 0;
                    usleep(3000 * 1000);
                    continue;
                }
                cnt++;
            }
            else
            {
                cnt = 0;
            }

#if 0
            ret = IMP_ISP_Tuning_GetAwbGlobalStatistics(vinum, &wb);
            if (ret == 0)
            {
                gr_gain = wb.statis_gol_gain.rgain;
                gb_gain = wb.statis_gol_gain.bgain;
                printf("---------------AwbGlobalStatistics---------------\n");
                PRT_NORMAL("gb_gain: %f\n", gb_gain);
                PRT_NORMAL("gr_gain: %f\n", gr_gain);
                printf("-------------------------------------------------\n");
            }
#endif
        }
        usleep(200 * 1000);
    }

    DEF_COM_EXIT_THREAD(g_ComThread_Num);
}

void Ext_SdkNightMonitor_Init(void)
{
    pthread_t id;

    pthread_create(&id, NULL, SdkNightMonitor_Thread,NULL);
    pthread_detach(id);
}
